package com.puti.doctor.controller;

import java.io.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.puti.common.core.domain.R;
import com.puti.common.redis.service.RedisService;
import com.puti.doctor.domain.HospitalDoctorInfo;
import com.puti.doctor.domain.util.AliyunOSSUtil;
import com.puti.doctor.domain.util.RandomNumUtil;
import com.puti.doctor.service.IHospitalDoctorInfoService;
import com.puti.doctor.domain.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.puti.common.log.annotation.Log;
import com.puti.common.log.enums.BusinessType;
import com.puti.common.security.annotation.PreAuthorize;
import com.puti.common.core.web.controller.BaseController;
import com.puti.common.core.web.domain.AjaxResult;
import com.puti.common.core.utils.poi.ExcelUtil;
import com.puti.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 医院-医生-个人信息 医院-医生-个人信息Controller
 *
 * @author puti
 * @date 2021-04-21
 */
@CrossOrigin
@RestController
@RequestMapping("/info")
public class HospitalDoctorInfoController extends BaseController
{
//    private String secretId="AKID95sVu2S25sPNXSvWNzKOaz8z1MRHtA7p";     				//腾讯云对象存储的secretId
//    private String secretKey="anQpztWLxJhaGgpgjX3cgeNqkOmvqyIN";          			//腾讯云对象存储的secretKey
//    private String bucket="https://shanghai1808a-1305600492.cos.ap-shanghai.myqcloud.com";                             //所属地域
//    private String bucketName="shanghai1808a-1305600492";                       //存储桶名称
//    private String path="https://shanghai1808a-1305600492.coscd.myqcloud.com";    //访问域名
//    private String qianzhui="shanghai1808a-1305600492/img/";      						//上传目录

    @Autowired
    private IHospitalDoctorInfoService hospitalDoctorInfoService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询医院-医生-个人信息 医院-医生-个人信息列表
     */
    @PreAuthorize(hasPermi = "system:info:list")
    @GetMapping("/list")
    public TableDataInfo list(HospitalDoctorInfo hospitalDoctorInfo)
    {
        startPage();
        List<HospitalDoctorInfo> list = hospitalDoctorInfoService.selectHospitalDoctorInfoList(hospitalDoctorInfo);
        return getDataTable(list);
    }
    //登录
    @PostMapping("/loginDoctor")
    public R loginDoctor(@RequestBody HospitalDoctorInfo hospitalDoctorInfo){
        return hospitalDoctorInfoService.loginDoctor(hospitalDoctorInfo);
    }
    //上传图片
    @PostMapping("/uploadM")
    public String uploadM(MultipartFile file) throws IOException {
        String img = AliyunOSSUtil.uploadMultipartFile(file, "img");
        return img;
    }
    /*private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
    /**
     * 导出医院-医生-个人信息 医院-医生-个人信息列表
     */
    @Log(title = "医院-医生-个人信息 医院-医生-个人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HospitalDoctorInfo hospitalDoctorInfo) throws IOException
    {
        List<HospitalDoctorInfo> list = hospitalDoctorInfoService.selectHospitalDoctorInfoList(hospitalDoctorInfo);
        ExcelUtil<HospitalDoctorInfo> util = new ExcelUtil<HospitalDoctorInfo>(HospitalDoctorInfo.class);
        util.exportExcel(response, list, "医院-医生-个人信息 医院-医生-个人信息数据");
    }
    //接诊台-未接诊列表-开启接诊：选择开启接诊，客户端可查看到该医师；
    //接诊台-未接诊列表-关闭接诊：选择关闭接诊，客户端隐藏该医师；
    @PostMapping("/{doctorId}")
    public Result updInfo(@PathVariable("doctorId") Long doctorId,@RequestParam("doctorInquiryStatus") int doctorInquiryStatus){
        return hospitalDoctorInfoService.updInfo(doctorId,doctorInquiryStatus);
    }
    /**
     * 获取医院-医生-个人信息 医院-医生-个人信息详细信息
     */
    @GetMapping(value = "/{doctorId}")
    public AjaxResult getInfo(@PathVariable("doctorId") Long doctorId)
    {
        return AjaxResult.success(hospitalDoctorInfoService.selectHospitalDoctorInfoById(doctorId));
    }

    /**
     * 新增医院-医生-个人信息 医院-医生-个人信息
     */
    @Log(title = "医院-医生-个人信息 医院-医生-个人信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public R add(@RequestBody HospitalDoctorInfo hospitalDoctorInfo)
    {
        return R.fail(hospitalDoctorInfoService.insertHospitalDoctorInfo(hospitalDoctorInfo),"注册成功,待审核");
    }

    /**
     * 修改医院-医生-个人信息 医院-医生-个人信息
     */
    @Log(title = "医院-医生-个人信息 医院-医生-个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalDoctorInfo hospitalDoctorInfo)
    {
        return toAjax(hospitalDoctorInfoService.updateHospitalDoctorInfo(hospitalDoctorInfo));
    }

    /**
     * 删除医院-医生-个人信息 医院-医生-个人信息
     */
    @Log(title = "医院-医生-个人信息 医院-医生-个人信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{doctorIds}")
    public AjaxResult remove(@PathVariable Long[] doctorIds)
    {
        return toAjax(hospitalDoctorInfoService.deleteHospitalDoctorInfoByIds(doctorIds));
    }
    //注册
    @PostMapping("/insertDoctor")
    public R insertDoctor(@RequestBody HospitalDoctorInfo hospitalDoctorInfo){
        int i = hospitalDoctorInfoService.insertHospitalDoctorInfo(hospitalDoctorInfo);
        if(i==0){
            return R.fail("注册失败");
        }else {
            return R.ok(null,"注册成功,请等待审核");
        }
    }
    //判断验证码是否正确
    @GetMapping("/verify")
    private R verify(@RequestParam("phone") String phone,@RequestParam("code") String code){
        System.out.println(phone);
        if(phone!=null){
            Object o = redisTemplate.opsForValue().get(phone);
            if(o!=null && o!=""){
                if(o.equals(code)){
                    return R.ok(null,"验证成功!");
                }else{
                    return R.fail(null,"验证码错误!");
                }
            }else {
                return R.fail(null,"还未发送验证码");
            }
        }else{
            return R.fail(null,"请先写手机号哦");
        }
    }
    //发送验证码
    @GetMapping("/code")
    public R code(@RequestParam("phone")String phone){
        if(phone!=null){
            Object o = redisTemplate.opsForValue().get(phone);
            if (o!=null){
                return R.fail("未发送成功,已经发送过验证码了");
            }else {
                String code = RandomNumUtil.randomNumFor();
                redisTemplate.opsForValue().set(phone,code,1800, TimeUnit.SECONDS);
                return R.ok(code,"发送成功");
            }
        }else{
            return R.fail("没有手机号哦");
        }
    }
}
